package com.sq.imp;

import com.sq.abstractClass.MainText;
import com.sq.abstractClass.Text;

import javax.swing.*;
import java.awt.*;

public class MainTextImp extends MainText {
    MainTextImp(Text textImp) {
        super(textImp);

        addMouseWheelListener(e->{
            if (e.isControlDown()) {
                // Ctrl+滚轮：调整字体大小
                Font currentFont = textImp.getFont();
                int newSize = e.getWheelRotation() < 0
                        ? Math.min(currentFont.getSize() + 2, 128)  // 最大128pt
                        : Math.max(currentFont.getSize() - 2, 8);   // 最小8pt
                textImp.setFont(currentFont.deriveFont((float) newSize));
                e.consume();  // 消耗事件，避免同时触发滚动
            } else {
                // 普通滚轮：控制滚动条
                JScrollBar verticalBar = this.getVerticalScrollBar();
                int unitIncrement = verticalBar.getUnitIncrement(e.getWheelRotation() > 0 ? 1 : -1);
                verticalBar.setValue(verticalBar.getValue() + (unitIncrement * e.getWheelRotation()));
            }
        });
    }
}
